/**
 * @class Ext.app.SaveQueryWindow
 * @extends Ext.panel.Window
 */
Ext.define('QCategory', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'name', type: 'string'},
        {name: 'value',  type: 'string'}
    ]
});
                
Ext.define('Ext.app.SaveQueryWindow', {
    extend: 'Ext.window.Window',
    alias: 'widget.saveQueryWindow',

    id: 'saveQueryWindow',
    height: 210,
    width: 400,
    layout: 'border',
    items: [
        {
            xtype: 'toolbar',
            region: 'north',
            items: [
                {
                    text: this.translations.get("ACTION.VALIDATE"),
                    handler : this.functions.get("ACTION.SAVE_QUERY")
                },
                {
                    text: this.translations.get("ACTION.CANCEL"),
                    handler : function(button, e) {
                        var wind = button.up('saveQueryWindow');
                        if(wind != null) {
                            wind.close();
                        }
                    }
                }
            ]
        }, 
        {
            region: 'center',
            xtype: 'form',
            bodyPadding: 15,
            fieldDefaults: {
                msgTarget: 'side',
                labelWidth: 100
            },
            items: [
                {
                    xtype: 'combo',
                    fieldLabel: this.translations.get("HOME.CATEGORY"),
                    afterLabelTextTpl: '<span style="color:red;font-weight:bold">*</span>',
                    allowBlank: false,
                    blankText: this.translations.get("HOME.CATEGORY_MANDATORY_MSG"),
                    displayField: 'name',
                    valueField: 'value',
                    editable: false,
                    store: Ext.create('Ext.data.Store', {
                        model: 'QCategory',
                        proxy: {
                            type: 'ajax',
                            url: 'Categories.jsp',
                            reader: {
                                type: 'json',
                                root: 'categories'
                            }
                        }
                    }),
                    width: 350,
                    name: 'category'
                },
                {
                    xtype: 'textfield',
                    fieldLabel: this.translations.get("HOME.QUERY_CODE"),
                    afterLabelTextTpl: '<span style="color:red;font-weight:bold">*</span>',
                    allowBlank: false,
                    blankText: this.translations.get("HOME.QUERY_CODE_MSG"),
                    width: 350,
                    name: 'queryCode'
                },
                {
                    xtype: 'textfield',
                    fieldLabel: this.translations.get("HOME.QUERY_NAME"),
                    afterLabelTextTpl: '<span style="color:red;font-weight:bold">*</span>',
                    allowBlank: false,
                    blankText: this.translations.get("HOME.QUERY_NAME_MSG"),
                    width: 350,
                    name: 'queryName'
                },
                {
                    xtype: 'combo',
                    fieldLabel: this.translations.get("HOME.IS_ADMIN_QUERY"),
                    displayField: 'name',
                    valueField: 'value',
                    queryMode: 'local',
                    editable: false,
                    value: '0',
                    store: {
                        fields: ['value', 'name'],
                        data : [
                            {"value": "0", "name": this.translations.get("HOME.NO")},
                            {"value": "1", "name": this.translations.get("HOME.YES")}
                        ]
                    },
                    width: 350,
                    name: 'isAdminQuery'
                }
            ]
        }
    ],
    
    constructor: function (translations, functions) {
        this.translations = translations;
        this.functions = functions;
        
        this.callParent(arguments);
    },
    
    listeners: {
        beforeclose: function () {
            Ext.getBody().unmask();
        }
    },
    
    initComponent : function() {
        Ext.getBody().mask();
        
        this.title = this.translations.get("ACTION.SAVE_QUERY_AS");
        
        this.callParent();
    },
    
    getValue: function(selector) {
        var cmp = this.down(selector);
        return (cmp == null ? "" : cmp.getValue());
    },

    getData: function() {
        return {
            "category": this.getValue("[name=category]"), 
            "queryCode": this.getValue("[name=queryCode]"),
            "queryName": this.getValue("[name=queryName]"),
            "isAdminQuery": this.getValue("[name=isAdminQuery]")
        };
    }
});
